package ca.ubc.icapture.genapha.forms;

import icapture.SQLMgr;
import icapture.beans.DB.Gene;
import icapture.beans.DB.Phenotypes;
import icapture.beans.DB.SNP;
import java.util.ArrayList;
import java.util.HashSet;
import org.apache.struts.action.ActionMapping;

import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.validator.ValidatorForm;

public class GeneGeneInteractionForm extends ValidatorForm
{
    private String[] selectedSnps;
    private ArrayList<SNP> snps;
    private String[] conditioningSnps;
    private ArrayList<Phenotypes> phenotypes;
    private String selectedPhenotype;

    private String submitType;

    public GeneGeneInteractionForm()
    {
        super();
        phenotypes = SQLMgr.getPhenotypes(1);
    }

    @Override
    public ActionErrors validate(ActionMapping mapping, HttpServletRequest request)
    {
        ActionErrors errors = super.validate(mapping, request);

        // 2 snps must be selected. No more, no less.
        if (getSelectedSnps() == null || getSelectedSnps().length != 2)
        {
            errors.add("snpSelection", new ActionMessage("geneGeneParameters.error.notTwoSnps"));
        }
        else
        {
            // Verify that snps from more than 1 gene have been selected
            HashSet<Gene> geneList = new HashSet<Gene>();
            for (String rsNumber : getSelectedSnps())
            {
                geneList.add(SQLMgr.getSNP(rsNumber).getGene());
            }
            if (geneList.size() < 2)
            {
                errors.add("snpSelectionGene", new ActionMessage("geneGeneParameters.error.lessTwoGenes"));
            }
        }

        return errors;
    }

    /**
     * @return the selectedSnps
     */
    public String[] getSelectedSnps()
    {
        return selectedSnps;
    }

    /**
     * @param selectedSnps the selectedSnps to set
     */
    public void setSelectedSnps(String[] selectedSnps)
    {
        this.selectedSnps = selectedSnps;
    }

    /**
     * @return the snps
     */
    public ArrayList<SNP> getSnps()
    {
        return snps;
    }

    /**
     * @param snps the snps to set
     */
    public void setSnps(ArrayList<SNP> snps)
    {
        this.snps = snps;
    }

    /**
     * @return the conditioningSnps
     */
    public String[] getConditioningSnps()
    {
        return conditioningSnps;
    }

    /**
     * @param conditioningSnps the conditioningSnps to set
     */
    public void setConditioningSnps(String[] conditioningSnps)
    {
        this.conditioningSnps = conditioningSnps;
    }

    /**
     * @return the phenotypes
     */
    public ArrayList<Phenotypes> getPhenotypes()
    {
        return phenotypes;
    }

    /**
     * @param phenotypes the phenotypes to set
     */
    public void setPhenotypes(ArrayList<Phenotypes> phenotypes)
    {
        this.phenotypes = phenotypes;
    }

    /**
     * @return the selectedPhenotype
     */
    public String getSelectedPhenotype()
    {
        return selectedPhenotype;
    }

    /**
     * @param selectedPhenotype the selectedPhenotype to set
     */
    public void setSelectedPhenotype(String selectedPhenotype)
    {
        this.selectedPhenotype = selectedPhenotype;
    }

    /**
     * @return the submitType
     */
    public String getSubmitType()
    {
        return submitType;
    }

    /**
     * @param submitType the submitType to set
     */
    public void setSubmitType(String submitType)
    {
        this.submitType = submitType;
    }
}